package com.cslg.questionnaires.controller;

import com.cslg.questionnaires.entity.Student;
import com.cslg.questionnaires.entity.Teacher;
import com.cslg.questionnaires.service.ComputeFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class ComputeFormController {
    @Autowired
    private ComputeFormService computeFormService;

    @ResponseBody
    @GetMapping("/ComputeForm/{questionId}")
    public ModelAndView getComputeFormPage(@PathVariable int questionId, HttpSession session){
        String userType = (String)session.getAttribute("userType");
        if (userType.equals("Teacher")) {
            Teacher teacher = (Teacher) session.getAttribute("user");
            int teacherId = teacher.getId();
            HashMap<String,Object> msg = computeFormService.computeForm(questionId,userType,teacherId);
            return new ModelAndView("ComputeForm",msg);
        }else{
            // userType.equals("Student")
            Student student = (Student)session.getAttribute("user");
            int studentId = student.getId();
            HashMap<String,Object> msg = computeFormService.computeForm(questionId,userType,studentId);
            return new ModelAndView("ComputeForm",msg);
        }

    }
}
